package com.example.musicplayer;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.LocalMusicViewHolder>{
    Context context;
    List<LocalMusicBean>musicDatas;

    //RecyclerView点击监听事件
    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        public void OnItemClick(View view,int position);
    }

    public LocalMusicAdapter(Context context, List<LocalMusicBean>musicDatas) {
        this.context = context;
        this.musicDatas = musicDatas;
    }




    @NonNull
    @Override
    public LocalMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {     //创建viewHolder
        View view = LayoutInflater.from(context).inflate(R.layout.item_local_music,parent,false);
        LocalMusicViewHolder holder = new LocalMusicViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull LocalMusicViewHolder holder,int position) {     //绑定viewHolder
         LocalMusicBean musicBean = musicDatas.get(position);
         holder.idTv.setText(musicBean.getId());
         holder.songTv.setText(musicBean.getSong());
         holder.singerTv.setText(musicBean.getSinger());
         holder.albumTv.setText(musicBean.getAlbum());
         holder.timeTv.setText(musicBean.getDuration());

         holder.itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 onItemClickListener.OnItemClick(view,position);
             }
         });
    }

    @Override
    public int getItemCount() {     //返回条目数
        return musicDatas.size();
    }

    class LocalMusicViewHolder extends RecyclerView.ViewHolder{
        TextView idTv,songTv,singerTv,albumTv,timeTv;
        public LocalMusicViewHolder(View itemView){
            super(itemView);
            idTv=itemView.findViewById(R.id.item_local_number);
            songTv=itemView.findViewById(R.id.item_local_song);
            singerTv=itemView.findViewById(R.id.item_local_singer);
            albumTv=itemView.findViewById(R.id.item_local_album);
            timeTv=itemView.findViewById(R.id.item_local_duration);
        }
    }
}


